package com.jvup.gameshow.model.viewModel.impl;

import android.view.View;

import androidx.lifecycle.Observer;

import com.jvup.common.basic.ObjectUtil;
import com.jvup.common.constant.CacheStorageKey;
import com.jvup.common.utils.CacheStorageUtil;
import com.jvup.common.utils.CountDownTimerBuilder;
import com.jvup.gameshow.activity.MainActivity;
import com.jvup.gameshow.controller.MainController;
import com.jvup.gameshow.dialog.UserAgreementDialog;
import com.jvup.gameshow.model.viewModel.IMainViewModel;
import com.jvup.common.support.base.BaseViewModel;

import java.util.concurrent.TimeUnit;

public abstract class MainViewModel extends BaseViewModel<IMainViewModel> implements IMainViewModel {

    private UserAgreementDialog userAgreementDialog;

    public MainViewModel() {
        set("adBtnEnable", true);
        set("skipBtnVisible", View.VISIBLE);
        set("countdownText", "跳转");
    }

    //region    #附加管理 begin

    /**
     * 附加属性绑定
     * @param activity 所有者
     * @param controller 事件控制器
     */
    public void onAddition(MainActivity activity, MainController controller) {
        boolean isFirstShowUserAgreement = CacheStorageUtil.queryBoolean(CacheStorageKey.FIRST_SHOW_USER_AGREEMENT, true);
        if(isFirstShowUserAgreement) {
            userAgreementDialog = new UserAgreementDialog(activity, controller);
            userAgreementDialog.show();
        } else {
            playAd();
        }
    }

    /**
     * 清理函数，在Activity退出时，ViewModel生命周期到期会被回收，调用该事件，
     * 页面由于切换、横竖屏转换、内存回收等情况并不会影响到ViewModel的生命周期。
     */
    @Override
    protected void onCleared() {
        super.onCleared();
        if(ObjectUtil.nonNull(userAgreementDialog)) {
            userAgreementDialog.dismiss();
            userAgreementDialog = null;
        }
    }

    //endregion #附加管理 end

    /**
     * 添加一个关闭状态的观察，用于通知页面关闭，如果状态为true则希望观察者们能调起页面关闭行为。
     * @param observer 观察者
     */
    public void addCloseStateObserver(Observer<Boolean> observer){
        this.<Boolean>get("CloseState").observeForever(observer);
    }

    public void playAd() {

        if(ObjectUtil.nonNull(userAgreementDialog)) {
            userAgreementDialog.dismiss();
            userAgreementDialog = null;
        }

        CountDownTimerBuilder.build(4, 1, TimeUnit.SECONDS)
                .addTickCallBack(this::updateCountdown)
                .addFinishCallBack(this::finish)
                .start();
        proxy().setAdImgPath("https://mmbiz.qpic.cn/mmbiz_jpg/w7mpPZI3qSLodtPThjZFMMTpS2CmznCgHOaY0kicmof7hWDNnSqgqrxpwCfd57SA9CCiaTmuMA8jBq9pzXSQ3OTg/640?wx_fmt=jpeg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1");
    }

    /**
     * 正常入场
     */
    public void normalEnter() {
        proxy().updateCloseState(true);
    }

    public long updateCountdown(long countDownTime) {
        countDownTime = TimeUnit.MILLISECONDS.toSeconds(countDownTime);
        proxy().setCountdownText("跳过 " + countDownTime + "s");
        countDownTime--;
        return TimeUnit.SECONDS.toMillis(countDownTime);
    }

    public Long finish(Long countDownTime){
        updateCountdown(countDownTime);
        normalEnter();
        return countDownTime;
    }


}
